//
//  HistoryConstrutView.m
//  Runone
//
//  Created by runone02 on 16/12/7.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "HistoryConstrutView.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import "Masonry.h"
#import "Define.h"
#import "AppConstants.h"
#import "AppDelegate.h"
#import "PileInfo.h"
#import "CustomAnnotationView.h"
#import "CustomCallOutView.h"
#import "ROPointAnnotation.h"
#import "RightCodeTool.h"
#import "OneConst.h"
#import "StationModel.h"

@interface HistoryConstrutView () <MAMapViewDelegate>
@property (weak, nonatomic) IBOutlet MAMapView *mapView;

@property (weak, nonatomic) IBOutlet UIImageView *iconImageV;
@property (weak, nonatomic) IBOutlet UILabel *eventTypeLabel;
@property (weak, nonatomic) IBOutlet UILabel *pileNoLabel;
@property (weak, nonatomic) IBOutlet UILabel *diretionLabel;

@property (weak, nonatomic) IBOutlet UILabel *beginTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *endTimeLabel;

@property (weak, nonatomic) IBOutlet UILabel *construtUnitLabel;
@property (weak, nonatomic) IBOutlet UILabel *headerLabel;
@property (weak, nonatomic) IBOutlet UILabel *contactNOLabel;

@property (weak, nonatomic) IBOutlet UILabel *constructProjectLabel;

@property (weak, nonatomic) IBOutlet UIButton *trafficBtn;


@end

#pragma   ---------- 施工事件  历史事件详情

@implementation HistoryConstrutView
- (IBAction)trafficBtnClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        _mapView.showTraffic=YES;
    } else {
        _mapView.showTraffic=NO;
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.backgroundColor    = [UIColor colorWithRed:244.0/255 green:244.0/255 blue:244.0/255 alpha:1];
    _mapView.delegate       = self;
    _mapView.showsCompass   = NO;
    _mapView.showsScale     = NO;
    _iconImageV.image        = [UIImage imageNamed:@"event_construct_main_icon"];
    [self configTextColor];
}
#pragma mark -设置label的字体颜色
- (void) configTextColor {
    _eventTypeLabel.textColor       = kTableViewCellTextColor;
    _pileNoLabel.textColor    = kTableViewCellTextColor;
    _diretionLabel.textColor     = kTableViewCellTextColor;
    //    _lastTimeLabel.textColor     = kTableViewCellTextColor;
    _beginTimeLabel.textColor    = kTableViewCellTextColor;
    _endTimeLabel.textColor      = kTableViewCellTextColor;
    
    _construtUnitLabel.textColor   = kTableViewCellTextColor;
    _headerLabel.textColor       = kTableViewCellTextColor;
    _contactNOLabel.textColor       = kTableViewCellTextColor;
    
    _constructProjectLabel.textColor = kTableViewCellTextColor;
}

- (void) setCeModel:(CurrentEvent *)ceModel {
    _ceModel = ceModel;
    [self loadEventAnnotation];
}

- (void)setEventModel:(EventDetailModel *)eventModel {
    _eventModel = eventModel;
    
    _eventTypeLabel.text = _eventModel.EventBaseInfo.IncidentTypeName;
    NSString *rampStr;
    AppDelegate *myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    if (_eventModel.EventBaseInfo.TollStationUID) {
        for (StationModel *model in myDelegate.AllTollStationArray) {
            if ([model.TollStationUID isEqualToString:_eventModel.EventBaseInfo.TollStationUID]) {
                NSString *station     = [NSString stringWithFormat:@"%@施工",model.TollStationName];
                _diretionLabel.text = [NSString stringWithFormat:@"%@ %@",_eventModel.EventBaseInfo.RoadDerectionName, station];
            }
        }
        
        if ([_ceModel.RoadUID isEqualToString:@"8dd13d88-cb02-4471-876b-1dfe39419ee6"]) {
            _pileNoLabel.text    = [NSString stringWithFormat:@"%@+%@",_ceModel.BeginPile,_ceModel.BeginPileDistance];
            
        } else {
            NSString *LStr       = @"L";
            NSString *beginLKStr = [LStr stringByAppendingString:_ceModel.BeginPile];
            _pileNoLabel.text    = [NSString stringWithFormat:@"%@+%@",beginLKStr,_ceModel.BeginPileDistance];
        }
        
        
    }else {
        if (eventModel.EventBaseInfo.IsRamp) {
            rampStr = @"匝道施工";
        }else {
            rampStr = @"主线施工";
        }
        _diretionLabel.text = [NSString stringWithFormat:@"%@ %@",_eventModel.EventBaseInfo.RoadDerectionName, rampStr];
        
        if ([_ceModel.RoadUID isEqualToString:@"8dd13d88-cb02-4471-876b-1dfe39419ee6"]) {
            _pileNoLabel.text    = [NSString stringWithFormat:@"%@+%@至%@+%@",_ceModel.BeginPile,_ceModel.BeginPileDistance,_ceModel.EndPile,_ceModel.EndPileDistance];
            
        } else {
            NSString *LStr       = @"L";
            NSString *beginLKStr = [LStr stringByAppendingString:_ceModel.BeginPile];
            NSString *endLKStr   = [LStr stringByAppendingString:_ceModel.EndPile];
            _pileNoLabel.text    = [NSString stringWithFormat:@"%@+%@至%@+%@",beginLKStr,_ceModel.BeginPileDistance,endLKStr,_ceModel.EndPileDistance];
        }
        
    }
    
    //    _pileNOLabel.text    = [NSString stringWithFormat:@"%@+%ld至%@+%ld",_eventModel.EventBaseInfo.BeginPile,(long)_eventModel.EventBaseInfo.BeginPileDistance,_eventModel.EventBaseInfo.EndPile,(long)_eventModel.EventBaseInfo.EndPileDistance];
    
    //    NSString *rampStr;
    //    if (eventModel.EventBaseInfo.IsRamp) {
    //        rampStr = @"匝道";
    //    }else {
    //        rampStr = @"主线";
    //    }
    //    _diretionLabel.text = [NSString stringWithFormat:@"%@ %@",_eventModel.EventBaseInfo.RoadDerectionName, rampStr];
    
    //    _pileNoLabel.text    = [NSString stringWithFormat:@"%@+%ld至%@+%ld",_eventModel.EventBaseInfo.BeginPile,(long)_eventModel.EventBaseInfo.BeginPileDistance,_eventModel.EventBaseInfo.EndPile,(long)_eventModel.EventBaseInfo.EndPileDistance];
    
    //    if ([_ceModel.RoadUID isEqualToString:@"8dd13d88-cb02-4471-876b-1dfe39419ee6"]) {
    //        _pileNoLabel.text    = [NSString stringWithFormat:@"%@+%@至%@+%@",_ceModel.BeginPile,_ceModel.BeginPileDistance,_ceModel.EndPile,_ceModel.EndPileDistance];
    //
    //    } else {
    //        NSString *LStr       = @"L";
    //        NSString *beginLKStr = [LStr stringByAppendingString:_ceModel.BeginPile];
    //        NSString *endLKStr   = [LStr stringByAppendingString:_ceModel.EndPile];
    //        _pileNoLabel.text    = [NSString stringWithFormat:@"%@+%@至%@+%@",beginLKStr,_ceModel.BeginPileDistance,endLKStr,_ceModel.EndPileDistance];
    //    }
    
    _beginTimeLabel.text = [NSString stringWithFormat:@"%@",_eventModel.EventBaseInfo.OccurTime];
    
    if (_eventModel.EventBaseInfo.ActualEndTime ) {
        _endTimeLabel.text = [NSString stringWithFormat:@"%@",_eventModel.EventBaseInfo.ActualEndTime];
    }else {
        _endTimeLabel.text = @"";
    }
    
    _constructProjectLabel.text   = [NSString stringWithFormat:@"%@",_eventModel.Content];
    
    //*************公司
//    NSArray *array = [_eventModel.Department componentsSeparatedByString:@"公司"];
//    NSString *fisrtStr = array[0];
//    NSString *secondStr = array[1];
//    NSString *startStr = @"";
//    for (int i = 0; i < fisrtStr.length; i++) {
//        startStr = [startStr stringByAppendingString:@"*"];
//    }
//    NSString *aStr = [startStr stringByAppendingString:@"公司"];
//    NSString *bStr ;
//    if (startStr.length > 0) {
//        bStr = [aStr stringByAppendingString:secondStr];
//    }
//    _construtUnitLabel.text = [NSString stringWithFormat:@"施  工  单  位 :%@",bStr];
//    
//    NSInteger headerlength = _eventModel.Header.length;
    if (_eventModel.Department) {
         _construtUnitLabel.text = [NSString stringWithFormat:@"施  工  单  位 :%@",_eventModel.Department];
    }else {
    _construtUnitLabel.text = [NSString stringWithFormat:@"施  工  单  位 :"];
    }
    
//    // 林**
//    if (headerlength > 3) {
//        NSString *headerLastStr = @"";
//        NSInteger lastLen = headerlength - 3;
//        for (int i = 0; i <= lastLen ; i ++) {
//            headerLastStr =  [headerLastStr stringByAppendingString:@"*"];
//        }
//        NSString *fisrtNameStr = [_eventModel.Header substringWithRange:NSMakeRange(0, 2)];
//        NSString *nameStr = [fisrtNameStr stringByAppendingString:headerLastStr];
//        _headerLabel.text   = [NSString stringWithFormat:@"现 场 负 责 人 :%@",nameStr];
//    }else {
//        NSString *headerLastStr = @"";
//        NSInteger lastLen = headerlength - 1;
//        for (int i = 0; i <= lastLen ; i ++) {
//            headerLastStr =  [headerLastStr stringByAppendingString:@"*"];
//        }
//        NSString *fisrtNameStr = [_eventModel.Header substringWithRange:NSMakeRange(0, 1)];
//        NSString *nameStr = [fisrtNameStr stringByAppendingString:headerLastStr];
//        _headerLabel.text   = [NSString stringWithFormat:@"现 场 负 责 人 :%@",nameStr];
//    }
    if (_eventModel.Header) {
        _headerLabel.text   = [NSString stringWithFormat:@"现 场 负 责 人 :%@",_eventModel.Header];
    }else {
       _headerLabel.text   = [NSString stringWithFormat:@"现 场 负 责 人 :"];
    }
    
    //152**********
//    NSString *phoneLastStr = @"";
//    for (int i = 0; i < 8; i ++) {
//        phoneLastStr = [phoneLastStr stringByAppendingString:@"*"];
//    }
//    NSString *phoneFirstStr = [_eventModel.Telephone substringWithRange:NSMakeRange(0, 3)];
//    NSString *telStr = [phoneFirstStr stringByAppendingString:phoneLastStr];
//    _contactNOLabel.text  = [NSString stringWithFormat:@"联系电话:%@",telStr];
    if (_eventModel.Telephone) {
        _contactNOLabel.text  = [NSString stringWithFormat:@"联系电话:%@",_eventModel.Telephone];
    }else {
        _contactNOLabel.text  = [NSString stringWithFormat:@"联系电话:"];
    }
    
//    _construtUnitLabel.text = [NSString stringWithFormat:@"施  工  单  位 :%@",_eventModel.Department];
//    _headerLabel.text   = [NSString stringWithFormat:@"现 场 负 责 人 :%@",_eventModel.Header];
//    _contactNOLabel.text  = [NSString stringWithFormat:@"联系电话:%@",_eventModel.Telephone];
}

#pragma mark - 加载地图事件标注
- (void)loadEventAnnotation { //0.232529--0.300532--11.526650--24.709414--113.471923--326.931946
    
    AppDelegate *myDelegate = (AppDelegate*) [UIApplication sharedApplication].delegate;
    NSArray *pileArray =  myDelegate.PirArray;
    NSString *pileStr  = _ceModel.BeginPile;
    for (PileInfo *pileModel in pileArray) {
        if ([pileStr isEqualToString: pileModel.PileNo]) {
            _ceModel.Latitude = [NSString stringWithFormat:@"%f",pileModel.Latitude];
            _ceModel.Longitude =[NSString stringWithFormat:@"%f", pileModel.Longitude];
        }
    }
    ROPointAnnotation *roAnnotation = [[ROPointAnnotation alloc] init];
    float latitude = [_ceModel.Latitude floatValue];
    float longitude = [_ceModel.Longitude floatValue];
    
    MACoordinateSpan  span                  = MACoordinateSpanMake(0.232529, 0.300532);
    CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(latitude, longitude);
    MACoordinateRegion region = MACoordinateRegionMake(centerCoordinate, span);
    _mapView.region           = region;
    
    [_mapView setCenterCoordinate:centerCoordinate animated:NO];
    _mapView.zoomLevel = 11.214808;
    
    CLLocationCoordinate2D ret = CLLocationCoordinate2DMake(latitude, longitude);
    roAnnotation.coordinate = ret;
    _ceModel.Latitude = [NSString stringWithFormat:@"%f",ret.latitude];
    _ceModel.Longitude = [NSString stringWithFormat:@"%f",ret.longitude];
    roAnnotation.currentEventModel = _ceModel;
    [_mapView addAnnotation:roAnnotation];
    
}

- (NSArray *) getTheWantCameraDevice:(NSString *)roadDeretion {
    AppDelegate *myDelegate  = (AppDelegate*) [UIApplication sharedApplication].delegate ;
    NSArray *dataSourceArray = myDelegate.AllDeviceBaseArray ;
    NSMutableArray *wantCameraDevice = [[NSMutableArray alloc] init];
    
    for (DeviceBaseInfo *model in dataSourceArray) {
        if (model.DeviceType == DevicesTypeCamera && (model.Direction == [roadDeretion integerValue] || model.Direction == 3)) {
            [wantCameraDevice addObject:model];
        }
    }
    return wantCameraDevice;
}

- (NSArray *) getAllCameraGun {
    AppDelegate *myDelegate  = (AppDelegate*) [UIApplication sharedApplication].delegate ;
    NSArray *dataSourceArray = myDelegate.AllDeviceBaseArray ;
    NSMutableArray *wantCameraDevice = [[NSMutableArray alloc] init];
    
    for (DeviceBaseInfo *model in dataSourceArray) {
        if (model.DeviceType == DevicesTypeCamera ) {
            [wantCameraDevice addObject:model];
        }
    }
    return wantCameraDevice;
}

#pragma mark -地图代理
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation {
    if ([annotation isKindOfClass:[MAPointAnnotation class]]) {
        static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
        CustomAnnotationView *annotationView = (CustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
        if (annotationView == nil) {
            annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
        }
        annotationView.canShowCallout= NO;       //设置气泡可以弹出，默认为NO
        annotationView.calloutView.hidden = YES;
        if (_ceModel.IsRamp) {
            annotationView.image = [UIImage imageNamed:@"ramp_construction"];//施工图标
        }else {
            annotationView.image = [UIImage imageNamed:@"main_line_construction"];//施工图标
        }
        annotationView.centerOffset = CGPointMake(0, -annotationView.image.size.height / 2);
        return annotationView;
    }
    return nil;
}

- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view {
    CustomAnnotationView *customView = (CustomAnnotationView *)view;
    if (customView.CEModel) {
        customView.selected       = NO;
        customView.canShowCallout     = NO;
    }
    customView.calloutView.hidden = YES;
}
-(void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    NSLog(@"%f--%f--%f--%f--%f--%f",mapView.region.span.latitudeDelta,mapView.region.span.longitudeDelta,mapView.zoomLevel,mapView.region.center.latitude,mapView.region.center.longitude, mapView.rotationDegree);
}

@end
